package com.mlethe.android.demo.recyclerview.adapter;

import android.os.Bundle;
import android.util.TypedValue;
import android.view.View;
import android.view.animation.DecelerateInterpolator;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.ItemTouchHelper;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.mlethe.demo.recyclerview.adapter.R;
import com.mlethe.library.recyclerview.adapter.UniversalAdapter;
import com.mlethe.library.recyclerview.callback.ItemDragAndSwipeCallback;
import com.mlethe.library.recyclerview.decoration.GridItemDecoration;
import com.mlethe.library.recyclerview.decoration.LinearItemDecoration;

import java.util.ArrayList;
import java.util.List;

public class RecyclerAdapterSimpleActivity extends AppCompatActivity {

    private RecyclerView recyclerView;
    private SimpleAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recycler_adapter_simple);
        findViewById(R.id.changeTv).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                adapter.notifyItemMove(5, 6);
            }
        });
        recyclerView = findViewById(R.id.recyclerView);
//        StaggeredGridLayoutManager manager = new StaggeredGridLayoutManager(3, StaggeredGridLayoutManager.VERTICAL);
//        GridLayoutManager manager = new GridLayoutManager(this, 5);
        LinearLayoutManager manager = new LinearLayoutManager(this);
//        manager.setOrientation(LinearLayoutManager.HORIZONTAL);
        recyclerView.setLayoutManager(manager);
        GridItemDecoration itemDecorationion = new GridItemDecoration(this);
        LinearItemDecoration itemDecoration = new LinearItemDecoration(this);
        itemDecoration.setSize(7)
//                .setColor("#ff0000")
                .setDrawable(R.mipmap.ic_stcs_btm_line)
//                .setHorizontalShow(false)
//                .setVerticalShow(false)
                .setMarginSize(0, 7, 0, 7);
        recyclerView.addItemDecoration(itemDecoration);
        adapter = new SimpleAdapter();
        adapter.enableSwipeItem()
                .enableDragItem()
                .setDuration(500)
                .setInterpolator(new DecelerateInterpolator())
                .openLoadAnimation(UniversalAdapter.SCALEIN);
        adapter.setOnItemClickListener((v, item, position) -> {
            Toast.makeText(this, "" + position, Toast.LENGTH_SHORT).show();
        });
        ItemTouchHelper mItemHelper = new ItemTouchHelper(new ItemDragAndSwipeCallback(adapter));
        mItemHelper.attachToRecyclerView(recyclerView);
        recyclerView.setAdapter(adapter);
        adapter.setNewData(getData(100, 1));
    }

    /**
     * dp转px
     *
     * @param dp
     * @return
     */
    private float dp2px(float dp) {
        return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, getApplication().getResources().getDisplayMetrics());
    }

    public List<String> getData(int index, int type) {
        List<String> list = new ArrayList<>();
        for (int i = 0; i < index; i++) {
            if (type == 1) {
                list.add("刷新" + (i + 1));
            } else if (type == 2){
                list.add("重新加载" + (i + 1));
            } else {
                list.add("加载" + (i + 1));
            }
        }
        return list;
    }
}
